// 请求成功时返回
const normall = async (ctx, next) => {
  ctx.echo = (obj = {}) => {
    const { code = 200, msg = "操作成功", data = undefined, ...res } = obj
    ctx.body = {
      code,
      msg,
      data: {
        ...res
      },
    }
  }
  await next()
}

// 请求失败时返回
const errorFun = async (ctx, next) => {
  ctx.error = (obj = {}) => {
    const { code = 201, msg = "操作失败", data = undefined } = obj
    return ctx.app.emit(
      "error",
      {
        code,
        msg,
        data,
      },
      ctx
    )
  }
  await next()
}

export default {
  normall,
  errorFun,
}
